package sg.edu.nus.iss.vmcs.store;

import java.io.IOException;
import java.util.Iterator;

/*
 * Copyright 2003 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

/**
 * 
 * 
 * @version 3.0 5/07/2003
 * @author Olivo Miotto, Pang Ping Li
 * @author nyi nyi than - the destroyer
 */

public abstract class Store
{
	public final static int CASH = 1;
	public final static int DRINK = 2;
	
	public static int NONZERO =1;
	public static int DESC =2;

	protected int size;

	protected PropertyLoader propertyLoader;

	protected StoreItem items[];

	public Store()
	{
	}

	public Store(int itemn)
	{
		size = itemn;
		items = new StoreItem[size];
	}

	public void setStoreSize(int sz)
	{
		size = sz;
		items = new StoreItem[size];
	}

	public StoreItem[] getItems()
	{
		return items;
	}

	public void addItem(int idx, StoreItem object)
	{
		if ((idx >= size) || (idx < 0))
			return;
		items[idx] = object;
	}

	public StoreItem getStoreItem(int idx)
	{
		if ((idx >= size) || (idx < 0))
			return null;
		return items[idx];
	}

	public StoreObject findObject(String name)
	{
		String en;
		StoreObject so;
		int i;

		for (i = 0; i < size; i++)
		{
			if (items[i] == null)
				return null;
			so = items[i].getContent();
			if (so == null)
				return null;
			en = so.getName();
			if (en != null)
			{
				if (en.compareTo(name) == 0)
					return so;
			}
		}
		return null;
	}

	public void setQuantity(int idx, int qty)
	{
		System.out.println("Store: setQauntity- qty=" + qty);
		if ((idx >= size) || (idx < 0))
			return;
		items[idx].setQuantity(qty);
	}

	public int getStoreSize()
	{
		return size;
	}

	public void setPropertyLoader(PropertyLoader propertyLoader)
	{
		this.propertyLoader = propertyLoader;
	}

	public void saveProperty() throws IOException
	{
		int size = this.getStoreSize();
		this.propertyLoader.setNumOfItems(size);
		for (int i = 0; i < size; i++)
		{
			propertyLoader.setItem(i, this.getStoreItem(i));
		}
		propertyLoader.saveData();
	}
	public abstract Iterator createIterator(int type);
}
